// mircoApp/serviceCenter/comm/compContent/compContent.js
var app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true,
  },
  properties: {
    imgs:{
      type:Array,
      value:[]
    },
    content:{
      type:String,
      value:''
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    message:'',
    maxHeight:{ maxHeight: 140, minHeight: 140 },
    // 图片上传
    uploadUrl: "upload.uploadPic",
    fileList3: [],
    upNumMaxOther: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    textChange(event) {
      let val = event.detail;
      this.triggerEvent('textChange', {val:val})
    },
     //选择图片
     onRead(event) { //图片上传功能
      if (this.data.upNumMaxOther == 5 || this.data.upNumMaxOther >= 5) {
        wx.showToast({
          icon: "none",
          title: "图片数量已达到上限",
          duration: 1500,
        });
        return false;
      }
      let that = this;
      let urlStr = app.getNetAddresss(this.data.uploadUrl);
      wx.chooseMedia({
        count:  5 - this.data.upNumMaxOther,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          const tempFilePaths = res.tempFiles;
          console.log(tempFilePaths[0]);
          let photourl = tempFilePaths[0].tempFilePath;
          wx.showLoading({
            title: '加载中',
          })
          wx.uploadFile({
            url: urlStr,
            filePath: photourl,
            name: 'file',
            success(resdata) {
              let res = JSON.parse(resdata.data);
              wx.hideLoading();
              if (res.result == 1) {
                // that.setData({
                //   [itemName]: res.data.img_url
                // });
                let fileList3 = that.data.imgs;
                fileList3.push(res.data.img_url);

                that.triggerEvent('onRead', {imgs:fileList3})
                that.setData({
                  upNumMaxOther: that.data.upNumMaxOther + 1,
                  // fileList3: fileList3,
                });

              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            }
          });
        }
      });
    },    
    removeImg_1: function (e) {
      let index = e.currentTarget.dataset.index;
      // let fileList3 = this.data.fileList3;
      // fileList3.splice(index, 1);
      this.triggerEvent('removeImg', {index:index});
      // this.setData({
      //   fileList3: fileList3,
      // });
      this.setData({
        upNumMaxOther: this.data.imgs.length,
      });
    },
  }
})